package com.iwanghang.sqlitedemo.dialog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.iwanghang.sqlitedemo.Person;
import com.iwanghang.sqlitedemo.R;

/**
 * 修改对话框
 */
public class UpdateDialog extends Activity implements View.OnClickListener {
    /**
     * 修改请求代码
     */
    private static final int UPDATE_REQUESTCODE = 3;

    /**
     * id控件
     */
    private TextView idView;
    /**
     * 姓名控件
     */
    private EditText nameView;
    /**
     * 年龄控件
     */
    private EditText ageView;
    /**
     * 性别控件
     */
    private RadioGroup sexView;

    private String sexName;
    private int intAge;
    private RadioButton mMale;
    private RadioButton mFemale;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);    //去掉标题栏
        setContentView(R.layout.update_dialog);
        init();
    }

    private void init() {
        findViewById(R.id.update_confirm).setOnClickListener(this);
        findViewById(R.id.update_cancel).setOnClickListener(this);
        idView = (TextView) findViewById(R.id.update_edit_id);
        if (getIntent() != null && getIntent().hasExtra("personId")) {
            int id = getIntent().getIntExtra("personId", -1);
            if (id > 0) {
                idView.setText(String.valueOf(id));
            }
        }
        nameView= (EditText) findViewById(R.id.update_edit_name);
        if (getIntent() != null && getIntent().hasExtra("personName")) {
            String name= getIntent().getStringExtra("personName");
                nameView.setText(String.valueOf(name));
        }
        ageView= (EditText) findViewById(R.id.update_edit_age);
        if (getIntent() != null && getIntent().hasExtra("personAge")) {
            int age = getIntent().getIntExtra("personAge", -1);
                ageView.setText(String.valueOf(age));
        }
        sexView = (RadioGroup) findViewById(R.id.update_edit_sex);
        if (getIntent() != null && getIntent().hasExtra("personSex")) {
            String sex= getIntent().getStringExtra("personSex");
            if(sex.equals("男")){
                mMale=(RadioButton)findViewById(R.id.mMale);
                mMale.setChecked(true);
                mMale.setText(sex);
            }
            if(sex.equals("女")){
                mFemale=(RadioButton)findViewById(R.id.mFemale);
                mFemale.setChecked(true);
                mFemale.setText(sex);
            }

        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.update_confirm:
                confirm(); // 确认修改
                break;
            case R.id.update_cancel:
                cancel(); // 取消修改
                break;
        }
    }

    /**
     * 确认修改
     */
    private void confirm() {
        int _id = Integer.valueOf(String.valueOf(idView.getText()));
        String name = String.valueOf(nameView.getText());
        if(name.toString().length() == 0){

            Toast.makeText(UpdateDialog.this,"请输入姓名", Toast.LENGTH_LONG).show();
            return;

        }

        String age=String.valueOf(ageView.getText());
        if(age.toString().length() == 0){
            Toast.makeText(UpdateDialog.this,"请输入年龄", Toast.LENGTH_LONG).show();
        }else{
            intAge = Integer.valueOf(String.valueOf(ageView.getText()));

        }

        sexView = (RadioGroup) super.findViewById(R.id.update_edit_sex);
        RadioButton radioButton = (RadioButton)findViewById(sexView.getCheckedRadioButtonId());



        if( null == radioButton) {

            Toast.makeText(UpdateDialog.this, "请选择性别", Toast.LENGTH_LONG).show();
            return;

        }else{
            sexName=radioButton.getText().toString();
        }
        Person person = new Person();
        person.set_id(_id);

        person.setName(name);
        person.setAge(intAge);
        person.setSex(sexName);
        Intent intent = new Intent();
        intent.putExtra("person", person);
        setResult(UPDATE_REQUESTCODE, intent);
        finish();
    }

    /**
     * 取消修改
     */
    private void cancel() {
        setResult(UPDATE_REQUESTCODE);
        finish();
    }
}
